<?php
function comprobarSession($username,$password)
{
    $username=strtolower($username);
    $username=pg_escape_string($username);
    $password=pg_escape_string($password);
    
    $r=true;

    $query="SELECT id,username,rol,password FROM usuarios WHERE username='".$username."' and password='".md5($password)."'";
    $result=pg_query($query);
    $arrUser=pg_fetch_array($result);

    if(pg_num_rows($result)<1){
        $r=false;
    }else{
        $_SESSION['username']=$username;
        $_SESSION['password']=$password;
        $_SESSION['rol']=$arrUser['rol'];
        $_SESSION['usuario_id']=$arrUser['id'];
    }
    return $r;
}

function generarRegistro($tabla,$campoCondicion,$condicion){
    
    abrirConexion();
    $query="DELETE FROM $tabla WHERE $campoCondicion='$condicion'";
    $result=pg_query($query);
    
    $query="SELECT max(id) as id FROM $tabla";
    $result=pg_query($query);
    $arr=pg_fetch_array($result);
    
    if($arr['id']==0 || $arr['id']==""){
        $id=1;
    }else{
        $id=$arr['id']+1;
    }
    
    $_SESSION[$tabla.'_id']=$id;
    
    $arr=array($campoCondicion=>"'$condicion'",'id'=>$id);
    $model=new model();
    $model->save($tabla, $arr);
    
    return $id;
    
}

function generateSessionVariable($nombre,$arr)
{
    $_SESSION['secciones'][]=$arr;
}

function comprobarRol(){
    
    if(isset($_SESSION['rol'])){
        $rol=$_SESSION['rol'];
        
        if($rol=="Estudiante"){
            header("Location:panelEstudiante.php");
        }
        
    }else{
        header("location:index.php");
    }
    
}


?>
